package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.ISetmealDishService;
import com.sky.service.ISetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author gcl
 * @since 2024-07-02
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {
    @Autowired
    private ISetmealDishService setmealDishService;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryService categoryService;

    @Override
    public PageResult getSetmealPageList(SetmealPageQueryDTO dto) {
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(!Objects.isNull(dto.getName()) && !dto.getName().isEmpty(),Setmeal::getName, dto.getName()).eq(!Objects.isNull(dto.getStatus()),Setmeal::getStatus, dto.getStatus()).eq(!Objects.isNull(dto.getCategoryId()), Setmeal::getCategoryId, dto.getCategoryId());
        Page<Setmeal> page = this.page(new Page<>(dto.getPage(), dto.getPageSize()), setmealLambdaQueryWrapper);
        List<Setmeal> setmealList = page.getRecords();
        List<SetmealVO> collect = setmealList.stream().map(setmeal -> {
            LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
            categoryLambdaQueryWrapper.eq(Category::getId, setmeal.getCategoryId());
            Category one = categoryService.getOne(categoryLambdaQueryWrapper);
            SetmealVO setmealVO = BeanUtil.copyProperties(setmeal, SetmealVO.class);
            setmealVO.setCategoryName(one.getName());
            return setmealVO;
        }).collect(Collectors.toList());
        return new PageResult(page.getTotal(), collect);
    }

    @Override
    @Transactional
    public void addSetmeal(SetmealDTO dto) {
        Setmeal setmeal = BeanUtil.copyProperties(dto, Setmeal.class);
        this.save(setmeal);
        addSetmealAndDish(dto, setmeal);
    }

    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO dto) {
        Setmeal setmeal = BeanUtil.copyProperties(dto, Setmeal.class);
        this.updateById(setmeal);
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
        addSetmealAndDish(dto, setmeal);
    }

    @Override
    public SetmealVO getSetmealById(Long id) {
        SetmealVO setmealVO = BeanUtil.copyProperties(setmealMapper.selectById(id), SetmealVO.class);
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    @Override
    public void updateSetmealStatus(Integer status,Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        this.updateById(setmeal);
    }

    private void addSetmealAndDish(SetmealDTO dto, Setmeal setmeal) {
        List<SetmealDish> collect = dto.getSetmealDishes().stream().map(setmealDish -> {
            SetmealDish setmealDish1 = BeanUtil.copyProperties(setmealDish, SetmealDish.class);
            setmealDish1.setSetmealId(setmeal.getId());
            return setmealDish1;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(collect);
    }

}
